package com.pucong.gitee.database.backup.business.sys.controller;

import com.pucong.gitee.database.backup.business.sys.service.SysLoginService;
import com.pucong.gitee.database.backup.common.entity.sys.SysReturnUrl;
import com.pucong.gitee.database.backup.common.entity.sys.UserJwt;
import com.pucong.gitee.database.backup.common.entity.sys.UserLoginVo;
import com.pucong.gitee.database.backup.common.entity.sys.UserUpdatePwdVo;
import com.pucong.gitee.database.backup.common.entity.sys.dto.SysUserSettingUpdateDto;
import com.pucong.gitee.database.backup.utils.Res;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-01
 */
@RestController
@RequestMapping("/sys/login")
@Api(value = "用户登录-API ",tags = "01. 用户登录")
public class SysLoginController {

    @Autowired
    private SysLoginService sysLoginService;

    @GetMapping("/isLogin")
    @ApiOperation(value = "01校验是否登录")
    public Res<Boolean> isLogin(){
        Boolean aBoolean = sysLoginService.isLogin();
        return Res.ok(aBoolean);
    }

    @GetMapping("/getLoginUser")
    @ApiOperation(value = "02获取登录用户")
    public Res<UserJwt> getLoginUser(){
        UserJwt userJwt = sysLoginService.getLoginUser();
        return Res.ok(userJwt);
    }

//    @LicenseCheck
    @PostMapping("/login")
    @ApiOperation(value = "03登录")
    public Res<UserJwt> login(@Valid @RequestBody UserLoginVo userLoginVo){
        UserJwt userJwt = sysLoginService.login(userLoginVo);
        return Res.ok(userJwt);
    }

    @GetMapping("/loginOut")
    @ApiOperation(value = "04退出登录")
    public Res<SysReturnUrl> loginOut(){
        SysReturnUrl sysReturnUrl = sysLoginService.loginOut();
        return Res.ok(sysReturnUrl);
    }

    @PutMapping("/updateUserPwd")
    @ApiOperation(value = "05修改密码")
    public Res<SysReturnUrl> updateUserPwd(@Valid @RequestBody UserUpdatePwdVo userUpdatePwdVo){
        SysReturnUrl sysReturnUrl = sysLoginService.updateUserPwd(userUpdatePwdVo);
        return Res.ok(sysReturnUrl);
    }

    @GetMapping("/getLoginUserSetting")
    @ApiOperation(value = "06 获取 登录用户 自定义设置")
    public Res<String> getUserSettingById(){
        String userSetting = sysLoginService.getLoginUserSetting();
        return Res.ok(userSetting);
    }

    @PutMapping("/updateLoginUserSetting")
    @ApiOperation(value = "07 修改登录用户 的 自定义设置")
    public Res<Integer> updateUserSetting(@Validated @RequestBody List<SysUserSettingUpdateDto> systemUserSettingUpdateDtoList){
        sysLoginService.updateUserSetting(systemUserSettingUpdateDtoList);
        return Res.ok();
    }
}
